/*******************************************************************************
 *
 *               Copyright(c) 2005 by 中国移动通信有限公司.
 *
 *                       All rights reserved.
 *
 *******************************************************************************
 *     File Name         :  com.flute.search.strategy.FuzzyQueryStrategy.java
 *     Description       :
 * -----------------------------------------------------------------------------
 *     No.        Date              Revised by           Description
 *     0          2010-6-12         TangHongJun              Created
 ******************************************************************************/

package com.flute.framework.search.strategy;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Query;

import com.flute.tools.util.StringUtil;

/**
 * @author TangHongJun
 * 
 * vision 创建时间：2010-6-12
 */
public class FuzzySearchStrategy extends AbstractSearchStrategy {

	private float chineseAmbiguity = 0.50f;
	private float englishAmbiguity = 0.7f;

	public FuzzySearchStrategy() {

	}

	public void setChineseAmbiguity(float chineseAmbiguity) {
		this.chineseAmbiguity = chineseAmbiguity;
	}

	public void setEnglisghAmbiguity(float englisghAmbiguity) {
		this.englishAmbiguity = englisghAmbiguity;
	}

	@Override
	public Object getQuery(String string) {
		return getQuery(string, null, 1f);
	}

	@Override
	public Object getQuery(String string, String sorter) {
		return getQuery(string, sorter, 1f);
	}

	@Override
	public Object getQuery(String string, String sorter, float boost) {
		if (chineseAmbiguity == 0 && englishAmbiguity == 0) {
			chineseAmbiguity = 0.50f;
			englishAmbiguity = 0.70f;
		}
		string = standardize(string);
		if (StringUtil.containsEnglishOrNumber(string)) {
			Query q = new FuzzyQuery(new Term(fieldName, string),
					englishAmbiguity, 1);
			q.setBoost(boost);

			return q;
		} else {
			Query q = new FuzzyQuery(new Term(fieldName, string),
					chineseAmbiguity, 1);
			q.setBoost(boost);

			return q;
		}
	}

}
